#include "day2.h"
#include <CommonLib/Common/Common.h>
#include <osg/MatrixTransform>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>

extern osgViewer::Viewer *createViewer();

int day2()
{
    osg::ref_ptr<osgViewer::Viewer> rpViewer = createViewer();
    osg::ref_ptr<osg::Group> rpGroup = new osg::Group;
    osg::ref_ptr<osg::Node> rpCow = FeCommon::readNode("cow.osg");
    rpGroup->addChild(rpCow.get());
    {
        osg::ref_ptr<osg::MatrixTransform> rpMT = new osg::MatrixTransform;
        rpMT->setMatrix(osg::Matrix::translate(0, 0, 10));
        rpMT->addChild(rpCow.get());
        FeCommon::setColor(rpMT.get(), osg::Vec4(1.0, 0.0, 0.0, 1.0));
        rpGroup->addChild(rpMT.get());
    }
    {
        osg::ref_ptr<osg::MatrixTransform> rpMTRT = new osg::MatrixTransform;
        rpMTRT->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(90.0), osg::Vec3(0, 0, 1))*osg::Matrix::translate(10, 0, 0));
        rpMTRT->addChild(rpCow.get());
        FeCommon::setColor(rpMTRT.get(), osg::Vec4(0.0, 1.0, 0.0, 1.0));
        rpGroup->addChild(rpMTRT.get());
        osg::ref_ptr<osg::MatrixTransform> rpMTTR = new osg::MatrixTransform;
        rpMTTR->setMatrix(osg::Matrix::translate(10, 0, 0)*osg::Matrix::rotate(osg::DegreesToRadians(90.0), osg::Vec3(0, 0, 1)));
        rpMTTR->addChild(rpCow.get());
        FeCommon::setColor(rpMTTR.get(), osg::Vec4(0.0, 0.0, 1.0, 1.0));
        rpGroup->addChild(rpMTTR.get());
    }
    {
        osg::ref_ptr<osg::MatrixTransform> rpMTRT = new osg::MatrixTransform;
        rpMTRT->setMatrix(osg::Matrix::scale(10, 10, 10)*osg::Matrix::rotate(osg::DegreesToRadians(90.0), osg::Vec3(0, 0, 1))*osg::Matrix::translate(50, 0, 0));
        rpMTRT->addChild(rpCow.get());
        FeCommon::setColor(rpMTRT.get(), osg::Vec4(1.0, 1.0, 0.0, 1.0));
        rpGroup->addChild(rpMTRT.get());
    }
    rpViewer->setSceneData(rpGroup.get());
    rpViewer->addEventHandler( new osgViewer::StatsHandler );
    rpViewer->addEventHandler( new osgViewer::WindowSizeHandler );
    rpViewer->addEventHandler( new osgGA::StateSetManipulator(rpViewer->getCamera()->getOrCreateStateSet()));
    return rpViewer->run();
}
